package cn.edu.ctbu.sbadmin.teacher.webapi;

import cn.edu.ctbu.sbadmin.common.controller.BaseRestController;
import cn.edu.ctbu.sbadmin.common.utils.MQuery;
import cn.edu.ctbu.sbadmin.common.utils.MQueryHelper;
import cn.edu.ctbu.sbadmin.common.utils.PageUtils;
import cn.edu.ctbu.sbadmin.common.utils.R;
import cn.edu.ctbu.sbadmin.lablog.dao.LabLogInfoDAO;
import cn.edu.ctbu.sbadmin.lablog.domain.DimClassDO;
import cn.edu.ctbu.sbadmin.lablog.domain.LabLogInfoDO;
import cn.edu.ctbu.sbadmin.lablog.service.DimClassService;
import cn.edu.ctbu.sbadmin.lablog.service.LabLogInfoService;
import cn.edu.ctbu.sbadmin.lablog.tools.lablogHelper;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("webapi/teacher/labloginfo")
public class TeacherLogInfoRestController extends BaseRestController {


    @Autowired
    LabLogInfoService labLogInfoService;


    @Autowired
    DimClassService dimClassService;

    @RequestMapping("/getByPage")
    @RequiresPermissions("teacher:labloginfo:list")
    public PageUtils list(@RequestParam("where") String where,
                          @RequestParam("pageSize") Integer pageSize,
                          @RequestParam("pageIndex") Integer pageIndex,
                          @RequestParam("sort") String sort,
                          @RequestParam("direct") String direct
    ) throws Exception {
        //首先，把where条件的参数转为QueryAndItems
//        其中,where条件需要我们自己组装.示例:
//        name=张^:like&time=2^:l g&dmtCreate=2017-01-01^^2018-01-01
//        这个表示:
//        (name like '%张%' )and ( time<2 ) and (dmtCreate between '2017-01-01' and '2018-01-01)

        Long userId=super.getUserId();
        where="teacherId="+userId+"&"+where;

        MQuery mQuery= MQueryHelper.GenQuery(where,pageSize,pageIndex,sort,direct,true);

        List<LabLogInfoDO> LabLogInfoDOList = labLogInfoService.listView(mQuery);

        int total=labLogInfoService.countView(mQuery);
        PageUtils pageUtils = new PageUtils(LabLogInfoDOList,total);

        return pageUtils;
    }

    @RequestMapping("/get")
    @RequiresPermissions("teacher:labloginfo:list")
    public R get(Long id) throws Exception{


        Long userId=super.getUserId();

        LabLogInfoDO labLogInfoDO=labLogInfoService.getView(id);

        if(labLogInfoDO!=null)
            return R.ok("data",labLogInfoDO);

        return R.error(-1,"找不到数据");
    }

    /**
     * 保存数据
     * @param labLogInfoDO
     * @return
     */
    @RequestMapping("/save")
    @RequiresPermissions("teacher:labloginfo:add")
    public R save(LabLogInfoDO labLogInfoDO) throws Exception{



        //先读取对应的班级信息，如果出错，则不作操作

        Long userId=super.getUserId();

        Long classId= labLogInfoDO.getClass_id();
        DimClassDO dimClassDO=dimClassService.getViewById(classId);
        if(dimClassDO==null){
            return R.error("出错了，没有正确的班级课程信息");
        }


        //把他们给那些缺省的值
        labLogInfoDO.setClassName(dimClassDO.getClassName());
        labLogInfoDO.setClassPersonNumber(dimClassDO.getClassPersonNumber());
        labLogInfoDO.setCourseName(dimClassDO.getCourseName());
        labLogInfoDO.setCourseType(dimClassDO.getCourseType());
        labLogInfoDO.setTermName(dimClassDO.getTermName());
        labLogInfoDO.setTeacherId(userId);
        labLogInfoDO.setCreateTime(new Date());

        labLogInfoDO=updateDateAndWeek(labLogInfoDO);




        if(labLogInfoService.save(labLogInfoDO)>0)
            return R.ok("data",labLogInfoDO);
        return R.error();

    }

    @RequestMapping("/update")
    @RequiresPermissions("teacher:labloginfo:edit")
    public R update(LabLogInfoDO labLogInfoDO)throws Exception{

        Long userId=super.getUserId();
        LabLogInfoDO oldInfo=labLogInfoService.getView(labLogInfoDO.getId());

        if(oldInfo!=null && oldInfo.getTeacherId()==userId){
            //可以更新
            labLogInfoDO.setCreateTime(oldInfo.getCreateTime());
            labLogInfoDO.setTeacherId(oldInfo.getTeacherId());
            labLogInfoDO=updateDateAndWeek(labLogInfoDO);
        }
        if(labLogInfoService.update(labLogInfoDO)>0){
            return R.ok("data",labLogInfoDO);
        }
        return R.error();
    }

    @RequestMapping("/batchDelete")
    @RequiresPermissions("teacher:labloginfo:remove")
    public R batchDelete(@RequestParam("ids[]") Long[] ids){
        List<LabLogInfoDO> labLogInfoDOS=labLogInfoService.findByIds(ids);

        Long userId=super.getUserId();
        if(labLogInfoDOS.size()>0){
            for(LabLogInfoDO labLogInfoDO:labLogInfoDOS){
                if(labLogInfoDO.getTeacherId()==userId){
                    labLogInfoService.delete(labLogInfoDO.getId());
                }
            }
        }
        return R.ok();
    }

    private LabLogInfoDO updateDateAndWeek(LabLogInfoDO labLogInfoDO) throws Exception{

        Date selectDate=labLogInfoDO.getCourseTime();
        if(selectDate==null){
            //默认使用当前日期
            selectDate=new Date();
            labLogInfoDO.setCourseTime(selectDate);
        }

        //从classid中取对应的学期的起始日期
        DimClassDO classDO=dimClassService.getViewById(labLogInfoDO.getClass_id());
        Date startDate=classDO.getStartDate();

        Integer[] rs= lablogHelper.converdate2Week(selectDate,startDate);
        labLogInfoDO.setWeek(rs[0]);//第几周
        labLogInfoDO.setDayOfWeek(rs[1]);//周几

        return labLogInfoDO;
    }
}
